package com.moive.news.tqnewsapplication.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.Button;

import com.moive.news.tqnewsapplication.R;


/**
 * Created by 84743 on 2016/10/19.
 */

public class TimeCountUtil extends CountDownTimer {
    private Button btn;
    private Context context;
    private String operateType;
    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public TimeCountUtil(long millisInFuture, long countDownInterval, Button button, Context context, String type) {
        super(millisInFuture, countDownInterval);
        this.btn=button;
        this.context=context;
        this.operateType=type;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        btn.setClickable(false);
        btn.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        btn.setText(millisUntilFinished / 1000 + "秒");

    }

    @Override
    public void onFinish() {
        btn.setTextColor(context.getResources().getColor(R.color.colorAccent));
        btn.setText("重新获取");
        btn.setClickable(true);
    }
//    public void starSend(String phone){
//        try {
//            JSONObject json = new JSONObject();
//            json.put("token", AppController.getInstance().getUserModel().getToken());
//            json.put("phone",phone);
//            json.put("send_type",operateType);
//            OkHttpUtils.post(UserApi.VERIFYCODE).tag(this).upString(Base64EncodeUtils.enCode(json+"")).execute(new OkManagerCallback() {
//                @Override
//                public void onSuccess(String s, Call call, Response response) {
//                    try {
//                        JSONObject jsonObject = Base64DecodeUtils.deCode(s);
//                        if (jsonObject.optInt("error_code")== BaseAPI.SUCCESS){
//                            start();//开始计时
//                            MakePToast.showOneToast(context,jsonObject.optString("msg"));
//                        }else {
//                            MakePToast.showOneToast(context,jsonObject+"");
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
